(function() {
  var crypto, express, iced, pgsql, router, uuid, __iced_k, __iced_k_noop;

  iced = require('iced-runtime');
  __iced_k = __iced_k_noop = function() {};

  express = require('express');

  pgsql = require('pg');

  uuid = require('uuid');

  crypto = require('crypto');

  router = express.Router();

  module.exports = router;

  router.all('/', function(req, res, next) {
    var client, dept, done, err, result, role, where_clause, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/users.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 13
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        where_clause = "( u.realname like $1 or u.loginname like $1 or u.email like $1 or u.mobile like $1 or u.address like $1 or u.notes like $1 )";
        where_clause += " and u.deleted = false";
        role = req.body.role || req.query.role;
        if (role && role.length > 0) {
          where_clause += " and u.role = '" + role + "'";
          res.locals.role = role;
        }
        dept = req.body.dept || req.query.dept;
        if (dept && dept.length > 0) {
          where_clause += " and u.dept = '" + dept + "'";
          res.locals.dept = dept;
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/users.coffee"
          });
          client.query("select * from web_roles", __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 35
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          res.locals.roles = result.rows;
          (function(__iced_k) {
            __iced_deferrals = new iced.Deferrals(__iced_k, {
              parent: ___iced_passed_deferral,
              filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/users.coffee"
            });
            client.query("select count(*) as count from web_users as u where " + where_clause, ['%' + res.locals.keyword + '%'], __iced_deferrals.defer({
              assign_fn: (function() {
                return function() {
                  err = arguments[0];
                  return result = arguments[1];
                };
              })(),
              lineno: 43
            }));
            __iced_deferrals._fulfill();
          })(function() {
            if (err) {
              done(client);
              return next(err);
            }
            res.locals.total = result.rows[0].count;
            (function(__iced_k) {
              __iced_deferrals = new iced.Deferrals(__iced_k, {
                parent: ___iced_passed_deferral,
                filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/users.coffee"
              });
              client.query("select u.*, r.name as role_name, r.root as role_root from web_users as u left join web_roles as r on r.uuid = u.role where " + where_clause + " order by serial offset $2::int limit $3::int", ['%' + res.locals.keyword + '%', res.locals.offset, res.locals.page_rows], __iced_deferrals.defer({
                assign_fn: (function() {
                  return function() {
                    err = arguments[0];
                    return result = arguments[1];
                  };
                })(),
                lineno: 54
              }));
              __iced_deferrals._fulfill();
            })(function() {
              if (err) {
                done(client);
                return next(err);
              }
              done();
              return res.render('users/index', {
                records: result.rows
              });
            });
          });
        });
      };
    })(this));
  });

  router.get('/new', function(req, res, next) {
    var client, done, err, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.locals.breadcrumbs.push({
      'title': '新增用户'
    });
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/users.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 64
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/users.coffee"
          });
          client.query("select * from web_roles", __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 68
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          done();
          res.locals.roles = result.rows;
          return res.render('users/new', {
            record: {}
          });
        });
      };
    })(this));
  });

  router.post('/new', function(req, res, next) {
    var client, done, err, k, password, result, sha1, user_uuid, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _len, _ref;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    _ref = ['loginname', 'password', 'realname', 'role'];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      k = _ref[_i];
      if (!req.body[k]) {
        return next(new Error("请求缺少参数[" + k + "]"));
      }
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/users.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 80
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          next(err);
        }
        user_uuid = uuid.v4();
        sha1 = crypto.createHash('sha1');
        sha1.update(user_uuid);
        password = sha1.update(req.body.password).digest('hex');
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/users.coffee"
          });
          client.query("insert into web_users ( ctime, uuid, loginname, password, role, realname, dept, birthday, gender, email, mobile, address, lock_enable, view_keys, view_trinfo, remove_notify, shortcuts, disabled, notes ) values ( CURRENT_TIMESTAMP, $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18 )", [user_uuid, req.body.loginname, password, req.body.role, req.body.realname, parseInt(req.body.dept), req.body.birthday || '1900-01-01', req.body.gender, req.body.email, req.body.mobile, req.body.address, req.body.lock_enable === 'on', req.body.view_keys === 'on', req.body.view_trinfo === 'on', req.body.remove_notify === 'on', req.body.shortcuts, false, req.body.notes], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 117
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          done();
          return res.redirect('/users');
        });
      };
    })(this));
  });

  router.get('/edit/:id', function(req, res, next) {
    var client, done, err, record, result, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.locals.breadcrumbs.push({
      'title': '修改用户信息'
    });
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/users.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 127
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/users.coffee"
          });
          client.query("select * from web_roles", __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 132
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          res.locals.roles = result.rows;
          (function(__iced_k) {
            __iced_deferrals = new iced.Deferrals(__iced_k, {
              parent: ___iced_passed_deferral,
              filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/users.coffee"
            });
            client.query("select * from web_users where serial = $1::int or uuid = $2::char", [parseInt(req.params.id) || 0, req.params.id], __iced_deferrals.defer({
              assign_fn: (function() {
                return function() {
                  err = arguments[0];
                  return result = arguments[1];
                };
              })(),
              lineno: 141
            }));
            __iced_deferrals._fulfill();
          })(function() {
            if (err) {
              done(client);
              return next(err);
            }
            done();
            if (result.rows.length !== 1) {
              return next(new Error("查无此用户记录[" + req.params.id + "]"));
            }
            record = result.rows[0];
            record.shortcuts = JSON.stringify(record.shortcuts);
            return res.render('users/edit', {
              record: record
            });
          });
        });
      };
    })(this));
  });

  router.post('/edit', function(req, res, next) {
    var client, done, err, k, new_remove_notify, new_view_keys, new_view_trinfo, password, remove_notify, result, role, sha1, view_keys, view_trinfo, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _len, _ref;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    _ref = ['serial', 'uuid', 'loginname', 'realname', 'role'];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      k = _ref[_i];
      if (!req.body[k]) {
        return next(new Error("请求缺少参数[" + k + "]"));
      }
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/users.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 157
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/users.coffee"
          });
          client.query("select role, view_keys, view_trinfo from web_users where serial = $1 and uuid = $2", [req.body.serial, req.body.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 164
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          if (result.rows.length === 0) {
            done();
            return next(new Error('未找到原始记录'));
          }
          role = result.rows[0].role;
          view_keys = result.rows[0].view_keys || false;
          view_trinfo = result.rows[0].view_trinfo || false;
          remove_notify = result.rows[0].remove_notify || false;
          new_view_keys = req.body.view_keys === 'on';
          new_view_trinfo = req.body.view_trinfo === 'on';
          new_remove_notify = req.body.remove_notify === 'on';
          (function(__iced_k) {
            __iced_deferrals = new iced.Deferrals(__iced_k, {
              parent: ___iced_passed_deferral,
              filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/users.coffee"
            });
            client.query("update web_users set loginname = $1, role = $2, realname = $3, dept = $4, birthday = $5, gender = $6, email = $7, mobile = $8, address = $9, lock_enable = $10, view_keys = $11, view_trinfo = $12, remove_notify = $13, shortcuts = $14, notes = $15 where serial = $16 and uuid = $17", [req.body.loginname, req.body.role, req.body.realname, parseInt(req.body.dept), req.body.birthday || '1900-01-01', req.body.gender, req.body.email, req.body.mobile, req.body.address, req.body.lock_enable === 'on', new_view_keys, new_view_trinfo, new_remove_notify, req.body.shortcuts, req.body.notes, req.body.serial, req.body.uuid], __iced_deferrals.defer({
              assign_fn: (function() {
                return function() {
                  err = arguments[0];
                  return result = arguments[1];
                };
              })(),
              lineno: 208
            }));
            __iced_deferrals._fulfill();
          })(function() {
            if (err) {
              done(client);
              return next(err);
            }
            password = req.body.password ? req.body.password : '';
            (function(__iced_k) {
              if (password.length > 0) {
                sha1 = crypto.createHash('sha1');
                sha1.update(req.body.uuid);
                password = sha1.update(password).digest('hex');
                (function(__iced_k) {
                  __iced_deferrals = new iced.Deferrals(__iced_k, {
                    parent: ___iced_passed_deferral,
                    filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/users.coffee"
                  });
                  client.query("update web_users set password = $1 where serial = $2 and uuid = $3", [password, req.body.serial, req.body.uuid], __iced_deferrals.defer({
                    assign_fn: (function() {
                      return function() {
                        err = arguments[0];
                        return result = arguments[1];
                      };
                    })(),
                    lineno: 221
                  }));
                  __iced_deferrals._fulfill();
                })(function() {
                  if (err) {
                    done(client);
                    return next(err);
                  }
                  return __iced_k();
                });
              } else {
                return __iced_k();
              }
            })(function() {
              (function(__iced_k) {
                if (role !== req.body.role || view_keys !== new_view_keys || view_trinfo !== new_view_trinfo || remove_notify !== new_remove_notify) {
                  (function(__iced_k) {
                    __iced_deferrals = new iced.Deferrals(__iced_k, {
                      parent: ___iced_passed_deferral,
                      filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/users.coffee"
                    });
                    client.query("delete from web_session as s where s.sess#>>'{userinfo, uuid}' = $1", [req.body.uuid], __iced_deferrals.defer({
                      assign_fn: (function() {
                        return function() {
                          err = arguments[0];
                          return result = arguments[1];
                        };
                      })(),
                      lineno: 234
                    }));
                    __iced_deferrals._fulfill();
                  })(function() {
                    if (err) {
                      done(client);
                      return next(err);
                    }
                    done();
                    if (req.session.userinfo.uuid === req.body.uuid) {
                      req.session.userinfo = null;
                    }
                    return res.redirect('/');
                    return __iced_k();
                  });
                } else {
                  return __iced_k();
                }
              })(function() {
                done();
                req.session.shortcuts = null;
                if (req.body.uuid === req.session.userinfo.uuid) {
                  req.session.userinfo.realname = req.body.realname;
                }
                return req.session.save(function(err) {
                  return res.redirect('/users');
                });
              });
            });
          });
        });
      };
    })(this));
  });

  router.post('/disable', function(req, res, next) {
    var client, done, err, k, result, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _len, _ref;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.type('json');
    _ref = ['serial', 'uuid', 'disabled'];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      k = _ref[_i];
      if (!req.body[k]) {
        return next(new Error("请求缺少参数[" + k + "]"));
      }
    }
    if (req.body.uuid === req.session.userinfo.uuid) {
      return next(new Error('出于安全考虑，您不可以禁用自己的帐号'));
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/users.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 263
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/users.coffee"
          });
          client.query("update web_users set disabled = $1, failed_cnt = 0, unlock_time = null where serial = $2 and uuid = $3", [req.body.disabled, req.body.serial, req.body.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 271
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          done();
          return res.json({
            succ: true,
            redirect: '/users'
          });
        });
      };
    })(this));
  });

  router.post('/delete', function(req, res, next) {
    var client, done, err, k, result, ___iced_passed_deferral, __iced_deferrals, __iced_k, _i, _len, _ref;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    res.type('json');
    _ref = ['serial', 'uuid'];
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      k = _ref[_i];
      if (!req.body[k]) {
        return next(new Error("请求缺少参数[" + k + "]"));
      }
    }
    if (req.body.uuid === req.session.userinfo.uuid) {
      return next(new Error('出于安全考虑，您不可以删除自己的帐号'));
    }
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/users.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 286
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/users.coffee"
          });
          client.query("delete from web_users where serial = $1 and uuid = $2", [req.body.serial, req.body.uuid], __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 293
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (!err) {
            done();
            return res.json({
              succ: true,
              redirect: '/users'
            });
          }
          (function(__iced_k) {
            __iced_deferrals = new iced.Deferrals(__iced_k, {
              parent: ___iced_passed_deferral,
              filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/users.coffee"
            });
            client.query("update web_users set deleted = true, deleted_no = max(deleted_no) + 1 where serial = $1 and uuid = $2", [req.body.serial, req.body.uuid], __iced_deferrals.defer({
              assign_fn: (function() {
                return function() {
                  err = arguments[0];
                  return result = arguments[1];
                };
              })(),
              lineno: 302
            }));
            __iced_deferrals._fulfill();
          })(function() {
            if (err) {
              done(client);
              return next(err);
            }
            done();
            return res.json({
              succ: true,
              redirect: '/users'
            });
          });
        });
      };
    })(this));
  });

}).call(this);
